package com.test.demos.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.test.demos.mapper.AppointmentsMapper;
import com.test.demos.pojo.dto.EvaluationsDTO;
import com.test.demos.pojo.entity.Appointments;
import com.test.demos.pojo.entity.Evaluations;
import com.test.demos.mapper.EvaluationsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.test.demos.service.EvaluationsService;

import java.util.Date;
import java.util.List;

import static com.test.demos.pojo.entity.table.EvaluationsTableDef.EVALUATIONS;
import static org.springframework.util.ObjectUtils.isEmpty;

/**
 * (Evaluations)表服务接口
 *
 * @author twt
 * @since 2025-04-14 16:26:25
 */
@Service
@Transactional
public class EvaluationsServiceImpl implements EvaluationsService {

    @Autowired
    private EvaluationsMapper evaluationsMapper;

    @Autowired
    private AppointmentsMapper appointmentsMapper;
    /**
     * 通过ID查询单条数据
     *
     * @param evaluationId 主键
     * @return 实例对象
     */
    @Override
    public Evaluations queryById(Integer evaluationId) {
        Evaluations select = evaluationsMapper.selectOneById(evaluationId);
        if (isEmpty(select)) {
            throw new RuntimeException("未查询到对应数据");
        }
        return select;
    }

    /**
     * 评价
     *
     * @param dto
     */
    @Override
    public void add(EvaluationsDTO dto) {
        //查询预约时间是否到了
        Integer appointmentId = dto.getAppointmentId();
        Appointments select1 = appointmentsMapper.selectOneById(appointmentId);
        if (select1.getAppointmentTime().after(new Date())) {
            throw new RuntimeException("预约时间未到，无法评价");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(EVALUATIONS.APPOINTMENT_ID.eq(dto.getAppointmentId()));
        Evaluations select = evaluationsMapper.selectOneByQuery(queryWrapper);
        if (select != null) {
            throw new RuntimeException("已经评价过，无法重复评价");
        }
        Evaluations evaluations = new Evaluations();
        BeanUtil.copyProperties(dto, evaluations);
        evaluationsMapper.insert(evaluations);
    }

    @Override
    public List<Evaluations> queryAll() {
        return evaluationsMapper.selectAll();
    }

    /**
     * 更新数据
     *
     * @param vo
     */
    @Override
    public void update(EvaluationsDTO vo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.where(EVALUATIONS.APPOINTMENT_ID.eq(vo.getAppointmentId()));
        Evaluations select = evaluationsMapper.selectOneByQuery(queryWrapper);
        if (isEmpty(select)) {
            throw new RuntimeException("未查询到对应数据");
        }
        BeanUtil.copyProperties(vo, select);
        int i = evaluationsMapper.update(select);
        if (i != 1) {
            throw new RuntimeException("更新数据失败");
        }
    }

    /**
     * 根据主键删除数据
     *
     * @param evaluationId
     */
    @Override
    public void deleteById(Integer evaluationId) {
        int i = evaluationsMapper.deleteById(evaluationId);
        if (i != 1) {
            throw new RuntimeException("删除数据失败");
        }
    }

}
